package com.cms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.entity.Site;
import com.cms.enums.SiteType;
import com.cms.mapper.SiteMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 站点表 服务实现类
 * </p>
 *
 * @author songfayuan
 * @since 2021-11-12
 */
@Service
public class SiteService extends ServiceImpl<SiteMapper, Site> {

    /**
     * 查找站点分页
     *
     * @param pageNumber
     *            页码
     * @param pageSize
     *            每页记录数
     * @return 站点分页
     */
    public Page<Site> findPage(Integer pageNumber, Integer pageSize){
        QueryWrapper<Site> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("createDate");
        return baseMapper.selectPage(new Page<>(pageNumber, pageSize),queryWrapper);
    }

    /**
     * 查找默认站点
     *
     */
    public Site findDefault(){
        QueryWrapper<Site> queryWrapper = new QueryWrapper();
        queryWrapper.eq("isDefault",true);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 查找所有站点
     *
     */
    public List<Site> findAll(){
        QueryWrapper<Site> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("createDate");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 查找站点
     *
     */
    public Site findByType(SiteType type, String cat, String domain){
        QueryWrapper<Site> queryWrapper = new QueryWrapper();
        queryWrapper.eq("type",type.ordinal());
        if(StringUtils.isNotBlank(cat)){
            queryWrapper.eq("cat",cat);
        }
        if(StringUtils.isNotBlank(domain)){
            queryWrapper.eq("domain",domain);
        }
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 查询数量
     *
     */
    public Long selectCount(String domain){
        QueryWrapper<Site> queryWrapper = new QueryWrapper();
        queryWrapper.eq("domain",domain);
        return baseMapper.selectCount(queryWrapper);
    }

}
